package com.wenjh.jdmzt.android.adapter;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;

import com.wenjh.jdmzt.util.Log4Android;

public abstract class BaseGridViewAdapter<T> extends BaseListAdapter<T> {
	protected int itemSize = -1;
	protected int leftPadding, rightPadding, topPadding, bottomPadding;
	
	private int lineWidth = 0;
	
	private Log4Android log = new Log4Android(this);

	public BaseGridViewAdapter(Context context, int lineWidth, int padding) {
		super(context);
		this.lineWidth = lineWidth;
		setItemPadding(padding);
		init();
	}
	
	public BaseGridViewAdapter(Context context, int padding) {
		super(context);
		setItemPadding(padding);
	}

	public BaseGridViewAdapter(Context context, List<T> objects, int lineWidth, int padding) {
		super(context, objects);
		this.lineWidth = lineWidth;
		setItemPadding(padding);
		init();
	}

	public void setItemPadding(int padding) {
		this.leftPadding = padding;
		this.rightPadding = padding;
		this.topPadding = padding;
		this.bottomPadding = padding;
	}
	
	public void setItemPadding(int leftPadding, int rightPadding, int topPadding, int bottomPadding) {
		this.leftPadding = leftPadding;
		this.rightPadding = rightPadding;
		this.topPadding = topPadding;
		this.bottomPadding = bottomPadding;
	}
	
	public void setLineWidth(int lineWidth) {
		this.lineWidth = lineWidth;
	}
	
	public abstract int getLineCount();

	public void init() {
		itemSize = lineWidth / getLineCount();
		itemSize = itemSize - leftPadding - rightPadding;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = getItemView(position, convertView, parent);

		if(itemSize > 0) {
			
			LayoutParams layoutParams = v.getLayoutParams();
			if(layoutParams == null) {
				layoutParams = new AbsListView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			}
			
			layoutParams.height = itemSize;
			layoutParams.width = itemSize;
			
			
			v.setLayoutParams(layoutParams);
			v.setPadding(leftPadding, topPadding, rightPadding, bottomPadding);
		}
		
		return v;
	}

	public abstract View getItemView(int position, View convertView, ViewGroup parent);
	
	
}
